package com.example.cybz.acl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cybz.acl.service.AdminService;
import com.example.cybz.acl.service.RoleService;
import com.example.cybz.common.result.Result;
import com.example.cybz.model.acl.Admin;
import com.example.cybz.utils.FileHashUtils;
import com.example.cybz.vo.acl.AdminQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Log4j2
@RestController
@Api(tags = "用户接口")
@RequestMapping("/admin/acl/user")
@RequiredArgsConstructor
public class AdminController {

    private final AdminService adminService;
    private final RoleService roleService;


    @ApiOperation(value = "为用户进行角色分配")
    @PostMapping("/toAssign")
    public Result<Object> assignRole(@RequestParam List<Long> roleIdList, @RequestParam Long adminId) {
        boolean savaAdminRole = roleService.savaAdminRole(adminId, roleIdList);
        return savaAdminRole ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "获取用户角色")
    @GetMapping("/toAssign/{admin}")
    public Result<Map<String, Object>> selectRoleByUserId(@PathVariable Long admin) {
        // 返回所有角色和用户的角色
        Map<String, Object> adminMap = roleService.selectRoleByUserId(admin);
        return Result.ok(adminMap);
    }

    @ApiOperation(value = "用户列表")
    @GetMapping("/{current}/{limit}")
    public Result<Page<Admin>> list(@PathVariable Long current, @PathVariable Long limit, AdminQueryVo adminQueryVo) {

        Page<Admin> adminPage = new Page<>(current, limit);
        Page<Admin> adminPage1 = adminService.selectAdimnPage(adminQueryVo, adminPage);

        return Result.ok(adminPage1);
    }

    @ApiOperation(value = "id查询用户")
    @GetMapping("/get/{id}")
    public Result<Admin> get(@PathVariable Long id) {
        Admin adminServiceById = adminService.getById(id);
        return Result.ok(adminServiceById);
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("/save")
    public Result<Object> save(@RequestBody Admin admin) throws IOException {
        // 加密密码
        admin.setPassword(FileHashUtils.md5(admin.getPassword()));
        boolean save = adminService.save(admin);
        return save ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改用户")
    @PutMapping("/update")
    public Result<Object> update(@RequestBody Admin admin) {
        boolean update = adminService.updateById(admin);
        return update ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/remove/{id}")
    public Result<Object> list(@PathVariable Long id) {
        boolean removeById = adminService.removeById(id);
        return removeById ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("batchRemove")
    public Result<Object> batchRemove(@RequestBody List<Long> idList){
        boolean removeByIds = adminService.removeByIds(idList);
        return removeByIds ? Result.ok() : Result.fail();
    }

}
